﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using FrienDev.BusinessRules;
using FrienDev.BusinessRules.ThumbnailHelper;
using System.IO;
using FrienDev.Data;

public partial class Controls_Club_InfoEdit : System.Web.UI.UserControl
{
    private static int clubid = -1; 
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Club club = null;
            if (HttpContext.Current.Request["clubId"] != null)
            {
                clubid = Convert.ToInt32(HttpContext.Current.Request["clubId"]);
            }
            if (clubid != -1)
            {
                int temp = ClubRules.Instance.GetMemberTypeIdByUserIdAndClubId(clubid, UserRules.Instance.GetCurrentUserId());
                if (temp >= ClubRules.m_3LevelMemberTypeId || temp == -1)
                {
                    editPanel.Visible = false;
                    pn_Privacy.Visible = true;
                }
                else
                {
                    club = ClubRules.Instance.GetClubByClubId(clubid);
                    if (club != null)
                    {
                        clubName.Text = club.ClubName;
                        clubDes.Text = club.Description;
                        BindClubType();
                        clubType.SelectedValue = club.ClubType.ToString();
                        if (club.AddRule)
                        {
                            addRule.SelectedIndex = 0;
                        }
                        else
                        {
                            addRule.SelectedIndex = 1;
                        }
                        if (club.PrivacyType)
                        {
                            privacy.SelectedIndex = 0;
                        }
                        else
                        {
                            privacy.SelectedIndex = 1;
                        }

                        InitDDLProvince();
                        province.SelectedValue = UserRules.Instance.GetProvinceByCityId(Convert.ToInt32(club.CityId.ToString())).ProvinceId.ToString();

                        InitDDLCity();
                        city.SelectedValue = club.CityId.ToString();
                    }
                }
            }
            
        }
    }
    private void BindClubType()
    {
        clubType.DataSource = ClubRules.Instance.GetClubTypes();
        clubType.DataTextField = "texts";
        clubType.DataValueField = "values";
        clubType.DataBind();
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        if (clubid != -1)
        {
            bool addrule;
            bool pri;
            if (addRule.SelectedIndex == 0)
            {
                addrule = true;
            }
            else
            {
                addrule = false;
            }
            if (privacy.SelectedIndex == 0)
            {
                pri = true;
            }
            else
            {
                pri = false;
            }
            if (RadioButtonList1.SelectedIndex == 0)
            {
                string logoFileName = "";
                if (SaveImages(ref logoFileName))
                {
                    ClubRules.Instance.UpdateClubInfoByClubId(clubid, Convert.ToInt32(city.SelectedValue), clubName.Text, clubDes.Text, logoFileName, addrule, pri, clubType.SelectedIndex);
                    Response.Redirect("/Club/Detail.aspx?clubId=" + clubid);
                }
            }
            else
            {
                ClubRules.Instance.UpdateClubInfoByClubId(clubid, Convert.ToInt32(city.SelectedValue), clubName.Text, clubDes.Text, "", addrule, pri, clubType.SelectedIndex);
                Response.Redirect("/Club/Detail.aspx?clubId=" + clubid);
            }
        }
        //bool addrule;
        //bool pri;
        //if (addRule.SelectedIndex == 0)
        //{
        //    addrule = true;
        //}
        //else
        //{
        //    addrule = false;
        //}
        //if (privacy.SelectedIndex == 0)
        //{
        //    pri = true;
        //}
        //else
        //{
        //    pri = false;
        //}
        //string logoFileName = "";
        //if (SaveImages(ref logoFileName))
        //{
        //    int clubId = ClubRules.Instance.AddClub(Convert.ToInt32(city.SelectedValue), clubName.Text, clubDes.Text, logoFileName, addrule, pri, clubType.SelectedIndex);
        //    ClubRules.Instance.AddClubMember(clubId, UserRules.Instance.GetCurrentUserId(), DateTime.Now, ClubRules.m_1LevelMemberTypeId);
        //    Response.Redirect("/Club/Detail.aspx?clubId=" + clubId);
        //}

    }

    private bool SaveImages(ref string logo)
    {
        ///'检查文件扩展名字
        errorAlertDiv.Visible = false;
        if (logoUpload.HasFile)
        {
            string fileExt = System.IO.Path.GetExtension(logoUpload.FileName).ToLower();
            string fileName = UserRules.Instance.GetCurrentUserId() + fileExt;

            if (fileExt == ".jpg" || fileExt == ".png" || fileExt == ".gif" || fileExt == ".bmp")
            {
                try
                {
                    logoUpload.SaveAs(System.Web.HttpContext.Current.Request.MapPath("~\\Upload\\Club\\") +
                        fileName);

                    //ThumbnailGenerater.CreateNewThumbnailImage(System.Web.HttpContext.Current.Request.MapPath("~\\Upload\\Club\\")
                    //     , fileName
                    //     , System.Web.HttpContext.Current.Request.MapPath("~\\Upload\\Club\\S\\")
                    //     , clubName.Text + ".png"
                    //     , 45
                    //     , 45
                    //     , true
                    //     , "");
                    logo = UserRules.Instance.GetCurrentUserId() + DateTime.Now.Year + DateTime.Now.Month + DateTime.Now.Day + DateTime.Now.Hour + DateTime.Now.Minute + DateTime.Now.Second + ".png";
                    ThumbnailGenerater.CreateNewThumbnailImage(System.Web.HttpContext.Current.Request.MapPath("~\\Upload\\Club\\")
                         , fileName
                         , System.Web.HttpContext.Current.Request.MapPath("~\\Upload\\Club\\")
                         , logo
                         , 105
                         , 135
                         , true
                         , "");
                    File.Delete(System.Web.HttpContext.Current.Request.MapPath("~\\Upload\\Club\\") + fileName);
                    //UserRules.Instance.UpdateCurUserImageSrc();

                    return true;

                }
                catch (Exception ex)
                {
                    errorAlertDiv.Visible = true;
                    lblError.Text = "ERROR: " + ex.Message.ToString();
                    return false;
                }
            }
            else
            {
                errorAlertDiv.Visible = true;
                lblError.Text = "Only .jpg/.png/.gif files allowed!";
            }

        }
        else
        {
            errorAlertDiv.Visible = true;
            lblError.Text = "You have not specified a file.";
        }
        return false;
    }

    private void InitDDLProvince()
    {
        province.DataSource = UserRules.Instance.GetProvinces();
        province.DataTextField = "ProvinceName";
        province.DataValueField = "ProvinceId";
        province.DataBind();
    }
    private void InitDDLCity()
    {
        city.DataSource = UserRules.Instance.GetCitiesByProvinceId(Convert.ToInt32(province.SelectedValue.ToString()));
        city.DataTextField = "CityName";
        city.DataValueField = "CityId";
        city.DataBind();
    }
    protected void province_SelectedIndexChanged(object sender, EventArgs e)
    {
        InitDDLCity();
    }

    //protected void handleCheckedChanged(object sender, EventArgs e)
    //{
    //    logoUpload.Visible = CheckBox1.Checked;
    //}

    protected void handleSelectedChanged(object sender, EventArgs e)
    {
        if (RadioButtonList1.SelectedIndex == 0)
        {
            logoUpload.Visible = true;
        }
        else
        {
            logoUpload.Visible = false;
        }
    }
}
